package com.moehu.back.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.moehu.back.annotation.AdminRestController;
import com.moehu.back.annotation.UserLoginToken;
import com.moehu.back.model.PagingModel;
import com.moehu.back.model.ResultModel;
import com.moehu.back.model.UpdateModel;
import com.moehu.back.model.request.UpdateListRequest;
import com.moehu.back.model.response.PagingResponseModel;
import com.moehu.back.service.impl.ToolServiceImpl;
import com.moehu.back.utils.AliYunOSS.AliyunOSSUtil;
import com.moehu.back.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/tool")
@AdminRestController
@Api(tags = "Tool 公共类接口")
public class AdminToolController {


    @Autowired
    ToolServiceImpl toolService;

    @UserLoginToken
    @ApiOperation(value="【管理后台使用】发布新的版本", notes="管理后台，管理员发布新版本使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "update_version", value = "app版本号，例如：1.0.1", required = true, dataType = "String"),
            @ApiImplicitParam(name = "update_version_code", value = "app版本的整数型，例如：101", required = true, dataType = "Number"),
            @ApiImplicitParam(name = "update_down_url", value = "下载地址，先调用图片上传接口", required = true, dataType = "String"),
            @ApiImplicitParam(name = "update_content", value = "更新的功能说明，输入的内容请以|隔开", required = true, dataType = "String"),
            @ApiImplicitParam(name = "update_force", value = "是否需要强制升级 1需要，0不需要", required = true, dataType = "Number"),
    })
    @PostMapping("/addNewUpdate")
    public ResultModel<String> addNewUpdate(@RequestBody UpdateModel update) {
        return toolService.addNewUpdate(update);
    }


    @UserLoginToken
    @ApiOperation(value="【管理后台使用】获取所有App版本", notes="管理后台，获取所有App版本历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "update_version", value = "app版本", required = true, dataType = "String", defaultValue="1"),
            @ApiImplicitParam(name = "update_force", value = "是否需要强制更新 1 需要，0不需要", required = true, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "page", value = "请求的页数", required = true, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = true, dataType = "Number", defaultValue="15"),
    })
    @PostMapping("/UpdateList")
    public ResultModel<PagingResponseModel<List<UpdateModel>>> UpdateList(@RequestBody UpdateListRequest page) {
        return toolService.getNewUpdate("all", page);
    }


    // TODO 还未掉接口
    @UserLoginToken
    @ApiOperation(value="【管理后台使用】删除App的版本", notes="管理后台，删除App的版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "update_id", value = "app版本的id", required = true, dataType = "String", defaultValue="1"),
    })
    @PostMapping("/deleteUpdate")
    public ResultModel<String> deleteUpdate(@RequestBody UpdateListRequest id) {
        return toolService.deleteUpdate(id.getUpdate_id());
    }


    @ApiOperation(value="【管理后台使用】文件上传", notes="用来上传图片，文件等，前端$(\"input\")[0].files[0]后将数据append进new FormData()中，然后使用ajax上传。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件的files数据，以FormData格式上传", required = true, dataType = "FormData"),
    })
    @PostMapping("/UploadFile")
    public ResultModel UploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResultUtils.Success("请选择文件");
        }
        String uploadUrl = AliyunOSSUtil.upLoad(file);
        JSONObject json = new JSONObject();
        json.put("img_url", uploadUrl);
        return ResultUtils.Success(json);
    }
}
